// 注入脚本到页面作用域，劫持 fetch 和 XMLHttpRequest
function injectScript(fn) {
    const script = document.createElement('script');
    script.textContent = '(' + fn.toString() + ')();';
    document.documentElement.appendChild(script);
    script.parentNode.removeChild(script);
}

injectScript(function () {
    // 劫持 fetch
    const originalFetch = window.fetch;
    window.fetch = async function (input, init) {
        let url = typeof input === 'string' ? input : input.url;
        if (url && url.includes('/api/web/v1/usercenter/login_check')) {
            if (init && init.body) {
                try {
                    let bodyObj = JSON.parse(init.body);
                    if (bodyObj.password !== undefined) {
                        bodyObj.password = '333333';
                        init.body = JSON.stringify(bodyObj);
                    }
                } catch (e) {
                    console.log(e);

                }
            }
        }
        return originalFetch.apply(this, arguments);
    };

    // 劫持 XMLHttpRequest
    const originalOpen = XMLHttpRequest.prototype.open;
    const originalSend = XMLHttpRequest.prototype.send;
    XMLHttpRequest.prototype.open = function (method, url) {
        this._targetUrl = url;
        return originalOpen.apply(this, arguments);
    };
    XMLHttpRequest.prototype.send = function (body) {
        if (this._targetUrl && this._targetUrl.includes('/api/web/v1/usercenter/login_check')) {
            try {
                let bodyObj = JSON.parse(body);
                if (bodyObj.password !== undefined) {
                    bodyObj.password = '333333';
                    body = JSON.stringify(bodyObj);
                }
            } catch (e) {
                console.log(e);
            }
        }
        return originalSend.call(this, body);
    };
});
